package textArea;

import java.awt.Component;
import java.awt.Container;
import java.awt.Point;

import javax.swing.JLabel;
import javax.swing.JTextPane;

import foldingborder.FoldingBorder;
import foldingborder.FoldingBorder.AwesomeTextFold;

/**
 * Custom text pane that accounts for the text
 * in AwesomeTextFold in getText().
 * @author Ashley Polkinghorn
 *
 */
public class AwesomeTextPane extends JTextPane {

	/**
	 * Get the text in the TextPane, including
	 * text folded in AwesomeTextFolds.
	 */
	@Override
	public String getText()
	{
		String text = super.getText();
		int compCount = getComponentCount();
		for (int i =0; i < compCount; i++){
			Component c1 = getComponent(i);
			Component c2 = ((Container)c1).getComponent(0);
			//If this component is equal to the one retrieved then we get remove it from the selection  
			if(c2 instanceof AwesomeTextFold){
				int start = viewToModel(new Point(c1.getX(),c1.getY()));

				text = text.replaceAll("\r", "");
				String first = text.substring(0, start);
				String middle = ((AwesomeTextFold)c2).toString();
				String last = text.substring(start);
				text = first+middle+last;
			}
		}


		return text;
	}
	
}
